package com.yunzhong.appointment.system.controller;

import com.github.pagehelper.PageInfo;
import com.yunzhong.appointment.entity.Department;
import com.yunzhong.appointment.entity.Departmenttype;
import com.yunzhong.appointment.system.service.DepartmentService;
import com.yunzhong.appointment.util.PageData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 
 * @className DepartmentController
 * @description 部门控制器
 * @author 石洪刚
 * @time 2017年11月14日 下午4:41:47
 */
@Controller
@RequestMapping("sys")
public class DepartmentController {

	@Autowired
	private DepartmentService departmentService;
	/**
	 * 
	 * @methodName listDepartment
	 * @description 查询部门信息
	 * @author 石洪刚
	 * @time 2017年11月14日 下午4:47:55
	 * @param request
	 * @param mm
	 * @return
	 */
	@RequestMapping("listDepartment")
	public String listDepartment(HttpServletRequest request, ModelMap mm){
		PageData pd = new PageData(request);
			List<Department> departmentList = departmentService.listDepartment(pd);
			PageInfo page = new PageInfo(departmentList);
			mm.put("page", page);
			mm.put("pd", pd);
		return "system/department/listdepartment";
	}
	/**
	 * 
	 * @methodName listDepartmentType
	 * @description 查询部门类型
	 * @author 石洪刚
	 * @time 2017年11月14日 下午4:48:32
	 * @param request
	 * @param mm
	 * @return
	 */
	@RequestMapping("listDepartmentType")
	public String listDepartmentType(HttpServletRequest request, ModelMap mm){
		PageData pd = new PageData(request);
		List<Departmenttype> dpTypeList = departmentService.listDepartmentType(pd);
		PageInfo page1 = new PageInfo(dpTypeList);
		mm.put("page1", page1);
		mm.put("pd", pd);
		return "system/department/listdepartment";
	}
	/**
	 * 
	 * @methodName pageDepartment
	 * @description 部门增加或修改页
	 * @author 石洪刚
	 * @time 2017年11月14日 下午4:49:52
	 * @param id
	 * @param mm
	 * @return
	 */
	@RequestMapping("pageDepartment")
	public String pageDepartment(String id, ModelMap mm){
		if(id!=null){
			Department department = departmentService.getDepartmentById(id);
			mm.put("department", department);
		}
		List<Departmenttype> departmentTypeList = departmentService.listDepartmentType();
		mm.put("departmentTypeList", departmentTypeList);
		return "system/department/pagedepartment";
	}
	/**
	 * 
	 * @methodName pageDepartmentType
	 * @description 部门类型的增加或修改页
	 * @author 石洪刚
	 * @time 2017年11月14日 下午4:50:10
	 * @param id
	 * @param mm
	 * @return
	 */
	@RequestMapping("pageDepartmentType")
	public String pageDepartmentType(String id, ModelMap mm){
		if(id!=null){
			Departmenttype departmentType = departmentService.getDepartmentTypeById(id);
			mm.put("departmentType", departmentType);
		}
		return "system/department/pagedepartmenttype";
	}
	/**
	 * 
	 * @methodName saveOrUpdateDepartment
	 * @description 增加或修改部门
	 * @author 石洪刚
	 * @time 2017年11月14日 下午4:50:30
	 * @param department
	 * @param redirectAttributes
	 * @return
	 */
	@RequestMapping("saveOrUpdateDepartment")
	public String saveOrUpdateDepartment(Department department, RedirectAttributes redirectAttributes){
		departmentService.saveOrUpdateDepartment(department);
		redirectAttributes.addFlashAttribute("firstObj", department);
		return "redirect:listDepartment";
	}
	/**
	 * 
	 * @methodName saveOrUpdateDepartmentType
	 * @description 部门类型的增加或修改
	 * @author 石洪刚
	 * @time 2017年11月14日 下午4:50:53
	 * @param departmentType
	 * @param redirectAttributes
	 * @return
	 */
	@RequestMapping("saveOrUpdateDepartmentType")
	public String saveOrUpdateDepartmentType(Departmenttype departmentType, RedirectAttributes redirectAttributes){
		departmentService.saveOrUpdateDepartmentType(departmentType);
		redirectAttributes.addFlashAttribute("firstObj1", departmentType);
		return "redirect:listDepartmentType";
	}
	/**
	 * 
	 * @methodName removeDepartment
	 * @description 删除部门
	 * @author 石洪刚
	 * @time 2017年11月14日 下午4:51:15
	 * @param ids
	 * @return
	 */
	@RequestMapping("removeDepartment")
	public String removeDepartment(String[] ids){
		departmentService.removeDepartment(ids);
		return "redirect:listDepartment";
	}
	/**
	 * 
	 * @methodName removeDepartmentType
	 * @description 删除部门类型
	 * @author 石洪刚
	 * @time 2017年11月14日 下午4:51:30
	 * @param ids
	 * @return
	 */
	@RequestMapping("removeDepartmentType")
	public String removeDepartmentType(String[] ids){
		departmentService.removeDepartmentType(ids);
		return "redirect:listDepartmentType";
	}
}
